/*
 * LevelLoader.java
 *
 * Created on 22. April 2007, 18:34
 *
 * To change this template, choose Tools | Template Manager
 * and open the template in the editor.
 */
package de.tor.loaders;

import com.golden.gamedev.GameEngine;
import com.golden.gamedev.GameObject;
import com.golden.gamedev.object.PlayField;
import com.golden.gamedev.object.Sprite;
import com.golden.gamedev.object.SpriteGroup;
import com.golden.gamedev.object.background.TileBackground;
import com.golden.gamedev.object.sprite.VolatileSprite;
import de.tor.obj.AbstractProjectile;
import de.tor.obj.AbstractWorldObject;
import de.tor.obj.Enemy;
import de.tor.obj.impl.Asteroid;
import de.tor.obj.action.ProjectileEnemyCollision;
import de.tor.obj.action.impl.SinusMovement;
import de.tor.obj.impl.EnemyFactory;
import de.tor.obj.impl.ProjectileFactory;
import de.tor.obj.impl.WorldObjectFactory;
import java.awt.Color;
import java.awt.Graphics2D;
import java.awt.geom.AffineTransform;
import java.awt.image.BufferedImage;
import java.awt.image.BufferedImageOp;
import java.io.File;
import java.util.Comparator;
import javax.imageio.ImageIO;

/**
 *
 * @author Charon
 */
public class Level extends GameObject {

    TileBackground background;
    GameEngine mParent;
    private Player player;
    PlayField playfield;
    Interface gameInterface;
    SpriteGroup PLAYER_PROJECTILE_GROUP, ASTEROID;
    ProjectileEnemyCollision collision, collision2;

    /** Creates a new instance of LevelLoader */
    public Level(GameEngine parent) {
        super(parent);
        mParent = parent;
    }

    public GameEngine getEngine() {
        return mParent;
    }

    public void initResources() {
        playfield = new PlayField();
        setMaskColor(Color.GREEN);
        int w = 15;
        int h = 20;
        //BufferedImage stars = new BufferedImage(parent.getWidth(), 1000, BufferedImage.TYPE_BYTE_GRAY);
        try {
            BufferedImage back = ImageIO.read(new File("./models/back1.png"));
            BufferedImage back1 = ImageIO.read(new File("./models/back2.png"));
            back = com.golden.gamedev.util.ImageUtil.resize(back, 1024, 768);
            back1 = com.golden.gamedev.util.ImageUtil.resize(back1, 1024, 768);
            BufferedImage[] tileImages = new BufferedImage[]{back, back1, back, back1};
            int[][] tiles = new int[1][4];
            tiles[0][0] = 0;
            tiles[0][1] = 1;
            tiles[0][2] = 2;
            tiles[0][3] = 3;
            background = new TileBackground(tileImages, tiles);
        } catch (Exception e) {
        }
        /* BufferedImage stars1 = new BufferedImage(parent.getWidth(), 1000, BufferedImage.TYPE_BYTE_GRAY);
        BufferedImage stars2 = new BufferedImage(parent.getWidth(), 1000, BufferedImage.TYPE_BYTE_GRAY);
        
        Graphics2D g2d = ((Graphics2D)stars.getGraphics());
        Graphics2D g2d1 = ((Graphics2D)stars1.getGraphics());
        Graphics2D g2d2 = ((Graphics2D)stars2.getGraphics());
        int starsCount = 1000;
        for(int i=0;i<starsCount;i++){
        g2d.setColor(Color.WHITE);
        double x = Math.random()*parent.getWidth();
        double y = Math.random()*1000;
        g2d.drawOval((int)x,(int)y,1,1);
        y = Math.random()*1000;
        g2d1.drawOval((int)x,(int)y,1,1);
        y = Math.random()*1000;
        g2d2.drawOval((int)x,(int)y,1,1);
        }
        BufferedImage[] tileImages = new BufferedImage[]{stars,stars1,stars2,stars,stars1,stars2,stars};
        int[][] tiles = new int[1][7];
        tiles[0][0] = 0;
        tiles[0][1] = 1;
        tiles[0][2] = 2;
        tiles[0][3] = 3;
        tiles[0][4] = 4;
        tiles[0][5] = 5;
        tiles[0][6] = 6;
        background = new TileBackground(tileImages, tiles);
        
        /*            new ParallaxBackground(new Background[] {
        new Background(parent.getWidth(), 1500),
        new ImageBackground(stars, getEngine().getWidth(), 500),
        new ImageBackground(stars, getEngine().getWidth(), 500),
        new ImageBackground(stars, getEngine().getWidth(), 500)
        });*/
        //10*960),*/


        playfield.setBackground(background);

        background.setLocation(0, 4 * 1000);
        playfield.setComparator(new Comparator() {

            public int compare(Object o1, Object o2) {
                Sprite s1 = (Sprite) o1,
                        s2 = (Sprite) o2;

                return (s1.getLayer() - s2.getLayer());
            }
        });

        ProjectileFactory.init(this);
        EnemyFactory.init(this);
        WorldObjectFactory.init(this);

        //player = new Player(this, getImages("resources/tiger_anim.png", 8, 1));
        player = new Player(this, getImages("resources/enemy3.png", 1, 1));
        player.setLayer(97);

        SpriteGroup PLAYER_GROUP = new SpriteGroup("Player");
        PLAYER_GROUP.add(player);
        AbstractWorldObject wo = WorldObjectFactory.factorWorldObject(WorldObjectFactory.STATION1);
        AbstractWorldObject woe = WorldObjectFactory.factorWorldObject(WorldObjectFactory.EXPLOSION1);


        ASTEROID = new SpriteGroup("Asteroid");
        wo.setLocation(1024 - wo.getWidth(), 6 * 1000);
        woe.setLocation(1024 - woe.getWidth(), 6 * 1000 + 400);
        woe.setLayer(99);
        wo.setVerticalSpeed(0.01);
        woe.setVerticalSpeed(0.01);

        Enemy w2 = EnemyFactory.factorEnemy(EnemyFactory.GUN3);
        w2.setRotation(200);
        w2.setTracking(player);
        ASTEROID.add(w2);
        SpriteGroup LEVEL_GROUP = new SpriteGroup("Level");
        wo.setLayer(1);
        wo.addChild(wo.getExtensionPoint(0), w2);
        LEVEL_GROUP.add(wo);
        LEVEL_GROUP.add(woe);
        playfield.addGroup(PLAYER_GROUP);
        playfield.addGroup(wo.getExtensionsGroup());
        playfield.addGroup(LEVEL_GROUP);

        for (int i = 0; i < 30; i++) {
            Asteroid as = (Asteroid) EnemyFactory.factorEnemy(EnemyFactory.ASTEROID3);
            SinusMovement lm = new SinusMovement(500, 6 * 1024, .5);
            as.setLocation(500, 6 * 1024);
            as.setMovement(lm);
            ASTEROID.add(as);
        }

        playfield.addGroup(ASTEROID);

        gameInterface = new Interface(parent, this, player);
        gameInterface.initResources();

        PLAYER_PROJECTILE_GROUP = new SpriteGroup("PlayerProjectiles");
        playfield.addGroup(PLAYER_PROJECTILE_GROUP);

        collision = new ProjectileEnemyCollision(this);
        collision2 = new ProjectileEnemyCollision(this);
        ProjectileEnemyCollision collision3 = new ProjectileEnemyCollision(this);
        playfield.addCollisionGroup(PLAYER_PROJECTILE_GROUP, ASTEROID, collision2);
        playfield.addCollisionGroup(PLAYER_GROUP, ASTEROID, collision);
        playfield.addCollisionGroup(PLAYER_GROUP, wo.getExtensionsGroup(), collision3);
    }

    public void firePlayerProjectileEvent(AbstractProjectile p) {
        PLAYER_PROJECTILE_GROUP.add(p);
    }

    public void fireProjectileProjectileEvent(SpriteGroup p) {
        ProjectileEnemyCollision nCollision = new ProjectileEnemyCollision(this);
        playfield.addGroup(p);
        playfield.addCollisionGroup(p, ASTEROID, nCollision);
    }

    public void fireExplosionEvent(VolatileSprite explosion) {
        if (explosion != null) {
            playfield.add(explosion);
        }
    }

    public double getLevelBottom() {
        return background.getY() + parent.getHeight() - gameInterface.getInterfaceHeight();
    }

    public double getLevelHeight() {
        return background.getHeight();
    }

    public double getLevelPosition() {
        return background.getY();
    }

    public void update(long elapsedTime) {
        background.move(0, -0.5);
        player.update(elapsedTime);
        playfield.update(elapsedTime);
        gameInterface.update(elapsedTime);
    }

    public PlayField getPlayField() {
        return playfield;
    }

    public void render(Graphics2D graphics2D) {
        playfield.render(graphics2D);
        gameInterface.render(graphics2D);
    }
}
